package com.ddwl.user.repository.impl;

import com.ddwl.user.dao.model.DeviceConnRecord;
import com.ddwl.user.repository.DeviceConnRecordRepositoryAdvance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

/**
 * @Author hemiaojun
 * @Date 2021/8/19 9:43
 */
public class DeviceConnRecordRepositoryImpl implements DeviceConnRecordRepositoryAdvance {

    @Autowired
    private MongoTemplate template;

    @Override
    public DeviceConnRecord getLast(String userId) {
        Query query = Query.query(Criteria.where("userId").is(userId)).with(Sort.by(Sort.Direction.DESC, "gmtCreated")).limit(1);
        return template.findOne(query, DeviceConnRecord.class);
    }

    @Override
    public void updateContinuousDay(DeviceConnRecord save) {
        template.updateMulti(Query.query(Criteria.where("id").is(save.getId())), Update.update("continuousDay", save.getContinuousDay()), DeviceConnRecord.class);
    }

}
